package com.fs.filter;

import com.fs.service.PreGlobalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


/**
 * @Author hyq
 * @Date 2019/9/29 18:56
 * @Description 前置过滤器
 */
//@Component
@Slf4j
//@RefreshScope
public class PreGlobalFilter implements GlobalFilter, Ordered {
    @Autowired
    private PreGlobalService preGlobalService;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("前置过滤器拦截=======================================================================》》开始");
        long start = System.currentTimeMillis();
        //在前置过滤器中验证 token,用户权限
        Mono<Void> mono = preGlobalService.filter(exchange, chain);
        long subTime = preGlobalService.printSub(start);
        log.info("前置过滤器拦截=======================================================================》》结束，耗时【{}】毫秒", subTime);
        return mono;
    }

    @Override
    public int getOrder() {
        return Integer.MIN_VALUE;
    }
}
